package test;

import java.text.ParseException;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MailDateFormat;

/**
 *
 * @author Tom
 */
public class test3 
{
    public static void main(String[] args)
    {
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
        try 
        {
            Session session = Session.getDefaultInstance(props, null);
            Store store = session.getStore();

            store.connect("imap.gmail.com", "user1.thunderpig@gmail.com",
                            "vinamilk");
            
            Folder inbox =  store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            FetchProfile fp = new FetchProfile();
            fp.add(FetchProfile.Item.ENVELOPE);
            Message[] messages=inbox.getMessages();
            inbox.fetch(messages, fp);
            for (Message message : messages)
            {
                String dateFormat=message.getHeader("Date")[0];
                System.out.println(dateFormat);               
                MailDateFormat format = new MailDateFormat();
                try {
                    Date d = format.parse(dateFormat);
                    System.out.println(d);
                } catch (ParseException ex) {
                    Logger.getLogger(test3.class.getName()).log(Level.SEVERE, null, ex);
                }
                //System.out.println(parser.parseDateTime(message.getHeader("Date")[0]));                
                System.out.println(message.getSubject());
                System.out.println(!message.getFlags().contains(Flags.Flag.SEEN));                        
                System.out.println("---------");
            }                                
        }
        catch (MessagingException ex) {
            Logger.getLogger(test3.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
